package com.atguigu.gulimall.member.service.impl;

import com.atguigu.gulimall.member.dao.MemberLevelDao;
import com.atguigu.gulimall.member.entity.MemberLevelEntity;
import com.atguigu.gulimall.member.exception.PhoneException;
import com.atguigu.gulimall.member.exception.UserNameException;
import com.atguigu.gulimall.member.vo.MemberUserLoginVo;
import com.atguigu.common.vo.SocialUserVo;
import com.atguigu.gulimall.member.vo.UserRegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.member.dao.MemberDao;
import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

	@Autowired
	MemberLevelDao memberLevelDao;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		IPage<MemberEntity> page = this.page(
				new Query<MemberEntity>().getPage(params),
				new QueryWrapper<MemberEntity>()
		);

		return new PageUtils(page);
	}

	@Override
	public void register(UserRegisterVo vo) {
		MemberEntity memberEntity = new MemberEntity();

		//	设置默认等级
		MemberLevelEntity levelEntity = memberLevelDao.getDefaultLevel();
		memberEntity.setLevelId(levelEntity.getId());

		//	设置其他默认信息
		//检查用户名和手机号是否唯一
		checkPhoneUnique(vo.getPhone());
		checkUserNameUnique(vo.getUserName());

		memberEntity.setNickname(vo.getUserName());
		memberEntity.setUsername(vo.getUserName());
		//密码md5加密
		BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
		String encode = bCryptPasswordEncoder.encode(vo.getPassword());
		memberEntity.setPassword(encode);
		memberEntity.setMobile(vo.getPhone());
		memberEntity.setGender(0);
		memberEntity.setCreateTime(new Date());

		this.baseMapper.insert(memberEntity);

	}

	@Override
	public MemberEntity login(MemberUserLoginVo vo) {
	//	查询用户名密码
		MemberEntity memberEntity = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username",
				vo.getLoginacct()).or().eq("mobile", vo.getLoginacct()));

		if (memberEntity != null){
			//获取数据库的password
			BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
			boolean matches = bCryptPasswordEncoder.matches(vo.getPassword(), memberEntity.getPassword());

			if (matches){
				//登录成功
				return memberEntity;

			}
		}
		return null;




	}

	//gitee登录
	@Override
	public MemberEntity login(SocialUserVo socialUserVo) {

		//具有登录和注册逻辑
		long uid = socialUserVo.getId();

		//1、判断当前社交用户是否已经登录过系统
		MemberEntity memberEntity = this.baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("social_uid", uid));

		if (memberEntity != null) {
			//这个用户已经注册过
			//更新用户的访问令牌的时间和access_token
			MemberEntity update = new MemberEntity();
			update.setId(memberEntity.getId());
			update.setAccessToken(socialUserVo.getAccess_token());
			update.setExpiresIn(socialUserVo.getExpires_in());
			this.baseMapper.updateById(update);

			memberEntity.setAccessToken(socialUserVo.getAccess_token());
			memberEntity.setExpiresIn(socialUserVo.getExpires_in());

			return memberEntity;
		} else {
			//2、没有查到当前社交用户对应的记录我们就需要注册一个
			MemberEntity register = new MemberEntity();

				//数据都在Vo 我的GItEE都给你查好了传过来的
				String name = socialUserVo.getName();

				String profileImageUrl = socialUserVo.getAvatar_url();

				register.setNickname(name);

				register.setHeader(profileImageUrl);
				register.setCreateTime(new Date());
				register.setSocialUid(String.valueOf(socialUserVo.getId()));
				register.setAccessToken(socialUserVo.getAccess_token());
				register.setExpiresIn(socialUserVo.getExpires_in());

				//把用户信息插入到数据库中
				this.baseMapper.insert(register);

			return register;
		}

	}

	private void checkUserNameUnique(String userName) {
		Integer userNameCount = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", userName));
		if (userNameCount > 0) {
			throw new UserNameException();
		}
	}

	private void checkPhoneUnique(String phone) {
		Integer phoneCount = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
		if (phoneCount > 0) {
			throw new PhoneException();
		}
	}

}